package com.jt.provider.interceptor;

import com.alibaba.cloud.sentinel.SentinelProperties;
import org.apache.juli.logging.Log;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
//基于Spring MVC模块提供的HandlerInterceptor接口规范定义拦截器，
//对特定请求进行拦截，例如检测请求访问时间
public class TimeAccessInterceptor implements HandlerInterceptor {

    @Override//preHandle定义在方法执行controller之前执行
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        LocalTime now= LocalTime.now();//JDK8中的时间对象
        int hour=now.getHour();//获取当前时间对应小时
        //System.out.println("hour="+hour);
//        log.info("hour {}",hour);
        if(hour<=6||hour>=12)
            throw new RuntimeException("请在6~12点进行访问");
        return true;
    }
}
